package com.konsung.ksdc.manager.shop;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.konsung.ksdc.R;
import com.konsung.ksdc.bean.netbean.OrderItemNetBean;
import com.konsung.ksdc.bean.netbean.OrderProductNetBean;
import com.konsung.ksdc.common.utils.Constants;
import com.konsung.ksdc.common.utils.StringUtils;
import com.konsung.ksdc.config.MyApplication;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 订单列表适配器
 * Created by Administrator on 2016/5/5 0005.
 */
public class OrderTypeAdapter extends BaseAdapter {
    public static final String BUY_LIST_DATA = "buy_list_data";
    public static final String ORDER_ID = "orderId";
    private List<OrderItemNetBean> orderItemNetBeanList;
    private Activity activity;

    /**
     * 构造方法
     * @param activity Activity
     * @param orderItemNetBeanList 订单集合
     */
    public OrderTypeAdapter(Activity activity, List<OrderItemNetBean> orderItemNetBeanList) {
        this.activity = activity;
        this.orderItemNetBeanList = orderItemNetBeanList;
    }

    @Override
    public int getCount() {
        if (orderItemNetBeanList != null) {
            return orderItemNetBeanList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return orderItemNetBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(MyApplication.getContext())
                    .inflate(R.layout.item_listview_order_type, null);
            new ViewHolder(convertView);
        }
        final ViewHolder holder = (ViewHolder) convertView.getTag();

        final OrderItemNetBean bean = orderItemNetBeanList.get(position);

        String orderId = bean.getOrderId();
        holder.tvTime.setText(orderId);

        final ArrayList<OrderProductNetBean> orderProductNetBeanList =
                (ArrayList<OrderProductNetBean>) bean.getConfs();

        holder.lvBuyList.setAdapter(new OrderBuyListAdapter(orderProductNetBeanList, bean
                .getStatus(), false));

        int count = orderProductNetBeanList.size();
        String countText = StringUtils.appendCount(count);
        holder.tvCountText.setText(countText);
        holder.tvPayText.setText(bean.getTotalPrice());

        // ### 根据订单状态显示不同的订单下面的界面 ###
        String status = bean.getStatus();
        switch (status) {
            case Constants.ORDER_TYPE_NOT_SEND:
                holder.tvState.setTextColor(ContextCompat.getColor(activity, R.color
                        .order_has_send));
                holder.tvState.setBackgroundResource(R.color.bg_normal);
                holder.tvState.setText(R.string.not_deliver);
                break;
            case Constants.ORDER_TYPE_NOT_COMMENT:
                holder.tvState.setTextColor(ContextCompat.getColor(activity, R.color
                        .order_has_send));
                holder.tvState.setBackgroundResource(R.drawable.bg_shape_express);
                holder.tvState.setText(R.string.evaluate);
                holder.tvState.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(activity,
                                OrderCommentActivity.class);
                        intent.putParcelableArrayListExtra(BUY_LIST_DATA,
                                orderProductNetBeanList);
                        String orderId = bean.getOrderId();
                        intent.putExtra(ORDER_ID, orderId);
                        activity.startActivity(intent);
                    }
                });
                break;
            case Constants.ORDER_TYPE_NOT_RECEIVED:
                holder.tvState.setTextColor(ContextCompat.getColor(activity, R.color
                        .order_has_send));
                holder.tvState.setBackgroundResource(R.color.bg_normal);
                holder.tvState.setText(R.string.not_receive);
                break;
            case Constants.ORDER_TYPE_CHECK:
                holder.tvState.setTextColor(ContextCompat.getColor(activity, R.color
                        .order_has_send));
                holder.tvState.setBackgroundResource(R.color.bg_normal);
                holder.tvState.setText(R.string.not_receive);
                break;
            case Constants.ORDER_TYPE_CLOSED:
                holder.tvState.setTextColor(ContextCompat.getColor(activity, R.color
                        .order_not_send));
                holder.tvState.setBackgroundResource(R.color.bg_normal);
                holder.tvState.setText(R.string.order_closed);
                break;
            case Constants.ORDER_TYPE_SUCCESS:
                holder.tvState.setTextColor(ContextCompat.getColor(activity, R.color
                        .order_not_send));
                holder.tvState.setBackgroundResource(R.color.bg_normal);
                holder.tvState.setText(R.string.success);
                break;
            default:
                break;
        }

        return convertView;
    }

    /**
     * 设置数据
     * @param data 订单集合
     */
    public void setData(List<OrderItemNetBean> data) {
        orderItemNetBeanList = data;
        notifyDataSetChanged();
    }

    /**
     * 适配器
     */
    static class ViewHolder {
        @Bind(R.id.tv_time)
        TextView tvTime;
        @Bind(R.id.tv_state)
        TextView tvState;
        @Bind(R.id.lv_buy_list)
        ListView lvBuyList;
        @Bind(R.id.tv_count_text)
        TextView tvCountText;
        @Bind(R.id.tv_pay_text)
        TextView tvPayText;

        /**
         * 防止复用，设置tag
         * @param view view
         */
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
            view.setTag(this);
        }
    }
}
